<?php

abstract class Sc_Db_Persistent extends Zend_Db_Table_Abstract
{
//    const FIELD_DATE_CREATED = 'dateCreated';

    protected $_instanceClass;

    public function getAll($conditions = array(), $order = NULL, $start = NULL, $end = NULL)
    {
        $select = $this->getQuery($conditions, $order);
        if ($start != NULL && $end != NULL) {
            $select->limit($end, $start);
        }
        if ($select instanceof Zend_Db_Table_Select) {
            $rows = $this->fetchAll($select);
        } else {
            $db = $this->getAdapter();
            $rows = $db->fetchAll($select);
        }
        $ret = $this->setRowsToArray($rows);

        return $ret;
    }

    public function get($conditions = array())
    {
        $select = $this->select()
            ->from($this);
        foreach ($conditions as $key => $value) {
            $select->where($key, $value);
        }
        $row = $this->fetchRow($select);
        if (empty($row) == FALSE) {
            if (empty($this->_instanceClass) == FALSE) {
                return new $this->_instanceClass($row);
            }
        }

        return FALSE;
    }

    public function insertRecord($record)
    {
        $data = $this->getRecordData($record);

        $ret = $this->insert($data);
        if ($ret > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function updateRecord($record)
    {
        $data = $this->getRecordForUpdate($record);
        $where = $this->getConditionForUpdate($record);
        $ret = $this->update($data, $where);

        return TRUE;
    }

    public function deleteRecord($record)
    {
        $data = $record->toArray();
        if (is_array($this->_primary) == TRUE) {
            foreach ($this->_primary as $column) {
                $where[$column . ' = ?'] = $data[$column];
            }
        } else {
            $where[$this->_primary . ' = ?'] = $data[$this->_primary];
        }
        return $this->delete($where);
    }

    public function lastInsertId()
    {
        return $this->_db->lastInsertId();
    }

    protected function getQuery($conditions, $order)
    {
        $select = $this->select()
            ->from($this);
        foreach ($conditions as $key => $value) {
            $select->where($key, $value);
        }
        if ($order != NULL) {
            $select->order($order);
        }

        return $select;
    }

    protected function setRowsToArray($rows)
    {
        $ret = array();
        if (empty($this->_instanceClass) == FALSE) {
            foreach ($rows as $row) {
                $ret[] = new $this->_instanceClass($row);
            }
        }

        return $ret;
    }

    protected function getRecordData($record)
    {
        $ret = $record->toArray();
//        $ret[self::FIELD_DATE_CREATED] = new Zend_Db_Expr('CURRENT_TIMESTAMP');

        return $ret;
    }

    protected function getRecordForUpdate($record)
    {
        return $record->toArray();
    }

    protected function getConditionForUpdate($record)
    {
        return array();
    }
}